//! \file pairfile.hpp
//! \brief Purewell Library Pair File Header
//! \author Yubin Lim (purewell@gmail.com)
//! \since 2008-01-25

#ifndef __PAIRFILE_HPP__
#define __PAIRFILE_HPP__

#include "file.hpp"

namespace purewell
{

//! \brief Pair file interface.
class PWPairFile : public PWFile
{
public:
	//! \brief Get second handle.
	//! \return Second file descriptor.
	inline int getHandle2(void) const
	{
		return m_fd2;
	}

	//! \brief Get handle.
	//! \param[in] idx Index. 0 or 1.
	//! \return File handle.
	inline int getHandle(size_t idx) const
	{
		return idx == 0 ? m_fd : m_fd2;
	}

public:
	//! \brief Attach file descriptors.
	//! \param[in] fd File descriptors.
	//! \return On success, true.
	virtual bool attach2(const int fd[2]);

	//! \brief Detach file descriptors.
	//! \param[out] fd File descriptors. If NULL, no operation.
	virtual void detach2(int* fd);

	virtual void destroy(void);

public:
	//! \brief Read data from file descriptor.
	//! \param[out] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[in] idx File descriptor index.
	//! \return On success, read data size.
	virtual ssize_t read(void* buf, size_t blen, size_t idx);

	//! \brief Write data to file descriptor.
	//! \param[in] buf Buffer.
	//! \param[in] blen Buffer length.
	//! \param[in] idx File descriptor index.
	//! \return On success, written data size.
	virtual ssize_t write(const void* buf, size_t blen, size_t idx);

public:
	explicit PWPairFile();
	virtual ~PWPairFile();

protected:
	virtual bool attach(int fd);
	virtual int detach(void);

protected:
	//! \brief Secondary file descriptor.
	int m_fd2;
};


};

#endif//!__PAIRFILE_HPP__

